package com.hxx.common.core.wcontour;

import com.alibaba.fastjson.JSON;
import com.hxx.common.core.wcontour.util.FeatureSurfaceUtil;
import com.hxx.common.core.wcontour.util.FeatureUtil;
import com.hxx.common.core.wcontour.util.GeoImageUtil;
import com.hxx.common.core.wcontour.base.TrainData;
import com.hxx.common.core.wcontour.enums.RainColorEnum;
import com.hxx.common.core.wcontour.read.GridJsonUtil;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.SchemaException;
import org.geotools.map.Layer;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;

public class TestRain {
    public static void main(String[] args) throws IOException, SchemaException {
        // 气象降水json数据,等值面裁切shp文件路径
        String jsonFile = "/Users/***/test/rain/世界时2022102500时数据.json";
        String boundryFile = "/Users/***/test/border_line/440000_borderline.shp";
        // 数据分层级别(数据间隙)
        double[] dataInterval = RainColorEnum.getValueArray();
        FeatureSurfaceUtil equiSurface = new FeatureSurfaceUtil();
        // 读取气象json文件，返回二维数组温度站点经纬度及温度值
        TrainData trainData = GridJsonUtil.read(new File(jsonFile));
        int[] size = trainData.getSize();
        System.out.println(String.format("开始进行等值面分析，经纬度个数和温度值数量为：%s, %s", size[0], size[1]));

        long start = System.currentTimeMillis();
//        FeatureCollection featureCollection = equiSurface.equiSurface(trainData.getData(), dataInterval,
//            trainData.get_X(), trainData.get_Y(), boundryFile);

//        CoordConverUtil.converX(trainData);
//        CoordConverUtil.converY(trainData);
        
        double[][] data2 = new double[trainData.getSize()[1] * trainData.getSize()[0]][3];
        double[][] data = trainData.getData();
        double[] _x = trainData.get_X();
        double[] _y = trainData.get_Y();
        int index = 0;
        for (int i = 0; i < _x.length; i++) {
            for (int j = 0; j < _y.length; j++) {
                double[] arr = new double[3];
                arr[0] = _x[i];
                arr[1] = _y[j];
                arr[2] = data[j][i];
                if(data[j][i] > 0 && _y[j] - 20.22 > 0.000001 && _x[i] - 109.68 > 0.000001){
                    System.out.println("v:" + data[j][i] + " x:" + _x[i] + " y:" + _y[j]);
                }
                data2[index++] = arr;
            }
        }
        FeatureCollection featureCollection = equiSurface.equiSurfaceGrid(trainData, dataInterval);

        System.out.println("等值面分析完成，总花时：" + (System.currentTimeMillis() - start));

        // 添加样式 透明度，颜色级别
        start = System.currentTimeMillis();
        float opacity = 1f;
        Map<Double, String> levelProps = RainColorEnum.getValueColorMap();
        Layer layer = FeatureUtil.addShapeLayer(featureCollection, levelProps, opacity);
        System.out.println("添加样式，总花时：" + (System.currentTimeMillis() - start));

        // 输出图片
        String imgPath = "/Users/***/test/rain/temp_440000.png";
        File file = new File(boundryFile);
        ShapefileDataStore shpDataStore = new ShapefileDataStore(file.toURI().toURL());
        // 设置编码，根据shp文件设置，一般中文操作系统的arcgis导出的是GBK，也可能是utf-8或其它，自行测试
        Charset charset = Charset.forName("GBK");
        shpDataStore.setCharset(charset);
        String typeName = shpDataStore.getTypeNames()[0];
        SimpleFeatureSource featureSource = shpDataStore.getFeatureSource(typeName);
        SimpleFeatureCollection fc = featureSource.getFeatures();
        // 获取shp边界，生成格网点
        double minX = fc.getBounds().getMinX();
        double minY = fc.getBounds().getMinY();
        double maxX = fc.getBounds().getMaxX();
        double maxY = fc.getBounds().getMaxY();
        double[] bbox = {minX, minY, maxX, maxY};
        int[] _xy = {1000, 800};
        GeoImageUtil.layerToPng(layer, bbox, _xy, imgPath);
        System.out.println("输出图片完成");

        // 输出geoJson
        String geojson = "/Users/***/test/rain/temp_440000.json";
        JSON geoJson = FeatureUtil.featureToGeoJson(featureCollection);
        Files.writeString(Paths.get(geojson), geoJson.toJSONString());
        System.out.println("输出geoJson完成");
    }
}
